﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WikiWiki.Models;

namespace WikiWiki.Controllers
{ 
    public class WikiController : Controller
    {
        private WikiContext db = new WikiContext();

        //
        // GET: /Wiki/

        public ViewResult Index()
        {
            return View(db.Wiki.ToList());
        }

        //
        // GET: /Wiki/Details/5

        public ViewResult Details(int id=0)
        {
            Wiki wiki = db.Wiki.Find(id);
            if (wiki == null)
                return HttpNotFound();
            return View(wiki);
        }

        //
        // GET: /Wiki/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Wiki/Create

        [HttpPost]
        public ActionResult Create(Wiki wiki)
        {
            if (ModelState.IsValid)
            {
                db.Wiki.Add(wiki);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(wiki);
        }
        
        //
        // GET: /Wiki/Edit/5
 
        public ActionResult Edit(int id = 0)
        {
            Wiki wiki = db.Wiki.Find(id);
            if (wiki == null)
            {
                return HttpNotFound();
            }
            return View(wiki);
        }

        //
        // POST: /Wiki/Edit/5

        [HttpPost]
        public ActionResult Edit(Wiki wiki)
        {
            if (ModelState.IsValid)
            {
                db.Entry(wiki).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(wiki);
        }

        //
        // GET: /Wiki/Delete/5
 
        public ActionResult Delete(int id=0)
        {
            Wiki wiki = db.Wiki.Find(id);
            if (wiki == null)
            {
                return HttpNotFound();
            }
            return View(wiki);
        }

        //
        // POST: /Wiki/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id=0)
        {            
            Wiki wiki = db.Wiki.Find(id);
            if (wiki == null)
            {
                return HttpNotFound();
            }
            db.Wiki.Remove(wiki);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        // SearchIndex
        //public ActionResult SearchIndex(string searchString)
        //{

        //    var wiki = from w in db.Wiki
        //                 select w;

        //    if (!String.IsNullOrEmpty(searchString))
        //    {
        //        wiki = wiki.Where(s => s.Title.Contains(searchString));
        //    }

        //    return View(wiki);
        //}

        public ActionResult SearchIndex(string wikiCategory, string searchString)
        {
            var CategoryLst = new List<string>();

            var CategoryQry = from w in db.Wiki
                           orderby w.Category
                           select w.Category;
            CategoryLst.AddRange(CategoryQry.Distinct());
            ViewBag.wikiCategory = new SelectList(CategoryLst);

            var wiki = from w in db.Wiki
                       select w;

            if (!String.IsNullOrEmpty(searchString))
            {
                wiki = wiki.Where(s => s.Title.Contains(searchString));
            }

            if (string.IsNullOrEmpty(wikiCategory))
                return View(wiki);
            else
            {
                return View(wiki.Where(x => x.Category == wikiCategory));
            }

        }


        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}